﻿using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using CandidateManager.App_Code.Helpers;
using WI.CandidateManager.Common.Data.Enums;

namespace CandidateManager.Controllers
{
    using System;
    using WI.CandidateManager.Business.Objects;

    public class LoginController : AnonymBaseController
    {

        /// <summary>
        /// Login Action
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// HTTP-POST Action for login.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                LoginBO loginBO = new LoginBO();
                try
                {
                    var result = loginBO.Login(model.Login, model.Password);
                    switch (result)
                    {
                        case UserRole.Admin:
                            FormsAuthentication.SetAuthCookie(model.Login, false);
                            return RedirectToAction("Index", new { area = "Admin", controller = "Home" });   //Need to define role of user (from BO.Logging) and redirect to special area
                        case UserRole.Candidate:
                            FormsAuthentication.SetAuthCookie(model.Login, false);
                            return RedirectToAction("Index", new { area = "Candidate", controller = "Home" });   //Need to define role of user (from BO.Logging) and redirect to special area
                        case UserRole.Coach:
                            FormsAuthentication.SetAuthCookie(model.Login, false);
                            return RedirectToAction("Index", new { area = "Coach", controller = "Home" });   //Need to define role of user (from BO.Logging) and redirect to special area
                    }
                }
                catch
                {
                    ModelState.AddModelError("", Resources.Login.InvalidLoginOrPassword); 
                    return View();
                }
            }
            return View();
        }

        /// <summary>
        /// Action for logout.
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Login");
        }

        /// <summary>
        /// Changes the culture(language) with validation.
        /// </summary>
        /// <param name="lang">The lang.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        public ActionResult ChangeCulture(string lang, string returnUrl)
        {
            HttpCookie newCookieCulture;
            if (Request.Cookies["lang"] != null)
            {
                newCookieCulture = LocalizationHelper.GetValidLanguageCookie(lang);
                HttpContext.Response.Cookies.Remove("lang");
                HttpContext.Response.Cookies.Add(newCookieCulture);
            }
            else
            {
                newCookieCulture = LocalizationHelper.GetValidLanguageCookie(lang);
                this.ControllerContext.HttpContext.Response.Cookies.Add(newCookieCulture);
            }
            CultureInfo ci = new CultureInfo(newCookieCulture.Value);
            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
            return Redirect(returnUrl);
        }
    }
}
